$package('Bdth.User');
Bdth.User = function () {
	var _box = null;
	var startTime = addDay(new Date(), -365).Format("yyyy-MM-dd") + " 00:00:00";
	var endTime = new Date().Format("yyyy-MM-dd") + " 23:59:59";
	var _this = {
		save: function () {
			_box.handler.save();
		},
		config: {
			event: {
				add: function () {
					_box.handler.add(function () {
						$.post(path + "/role/loadRoleList", function (data) {
							$("#roleId").append("<option value=''>请选择</option>");
							$.each(data, function (i, item) {
								$("#roleId").append("<option value='" + item.id + "'>" + item.roleName + "</option>");//新增
							});
						});
					});
				},
				edit: function () {
					var record = Utils.getCheckedRows();
					if (Utils.checkSelectOne(record)) {
						Bdth.progress();
						var data = {};
						var idKey = 'id'; // 主键名称
						data[idKey] = (record[0][idKey]);
						Bdth.getById('getId', data, function (result) {
							Bdth.closeProgress();
							$("#edit-win").dialog({
								href: path + '/view/user/form.html',
								width: 600,
								height: 365,
								modal: true,
								resizable: true,
								/*collapsible: true,
								 maximizable: true,*/
								onLoad: function () {
									$("#editForm").form('load', result.data);
									//$("#depId").val(result.depId);
									//$("#edit-department").val(result.depName);
									//$("#roleIdHidden").val(result.data.roleID);
									$("#roleId").removeClass('selectClass');
									$("#edit-department").removeClass('depInputClass');
									$.post(path + "/role/loadRoleList", function (data) {
										$("#roleId").append("<option value=''>请选择</option>");
										$.each(data, function (i, item) {
											$("#roleId").append("<option value='" + item.id + "'>" + item.roleName + "</option>");//新增
										});
										$("#roleId option[value='" + result.data.roleID + "']").attr("selected", true);
									});

								},
								title: '编辑',
								buttons: [{
									text: '保存',
									iconCls: 'fa fa-save',
									btnCls: 'topjui-btn',
									handler: _box.handler.save
								}/*, {
								 text: "关闭",
								 iconCls: "fa fa-close",
								 btnCls: "topjui-btn-danger",
								 handler: _box.handler.close
								 }*/],
							});

						});
					}
				},
				save: function () {
					_this.save();
				},
				search: function () {
					$("#startTimeSearch").attr("name", "startTime");
					$("#endTimeSearch").attr("name", "endTime");
					var startTime = $("#startTimeSearch").val();
					var endTime = $("#endTimeSearch").val();
					if (startTime >= endTime) {
						Bdth.closeProgress();
						$.iMessager.alert('提示', '开始时间不能大于等于结束时间', 'messager-warning');
						return false;
					}
					var param = $("#searchForm").serializeObject();
					$('#data-list').datagrid('reload', param);
				},
			},
			dataGrid: {
				title: '用户列表',
				url: 'dataList',
				queryParams: {"startTime": startTime, "endTime": endTime},
				columns: [[
					{field: 'id', checkbox: true, title: 'id'},
					{field: 'loginName', title: '登录名', width: 80, sortable: true},
					{field: 'userName', title: '名称', width: 120, sortable: true},
					{field: 'roleName', title: '角色', width: 120, sortable: true},
					{field: 'depName', title: '所属分组', width: 120, sortable: true},
					{field: 'tel', title: '手机号码', width: 120, sortable: true},
					{field: 'createTime', title: '创建时间', width: 120, sortable: true},
				]], toolbar: [
					{id: 'btnadd', text: '添加', btnType: 'add'},
					{id: 'btnedit', text: '修改', btnType: 'edit'},
					{id: 'btndelete', text: '删除', btnType: 'remove'}
				]
			}, action: {
				add_editForm: '/view/user/form.html',
				remove: 'fakeDelete'
			},
			wid_height: {
				width: 600,
				height: 365
			},
		},
		init: function () {
			_box = new DataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function () {
	Bdth.User.init();
});
// Grid 工具类
var Utils = {
	getCheckedRows: function () {
		return $('#data-list').datagrid('getChecked');
	},
	checkSelect: function (rows) {// 检查grid是否有勾选的行, 有返回 true,没有返回true
		var records = rows;
		if (records && records.length > 0) {
			return true;
		}
		Bdth.alert('警告', '未选中记录.', 'messager-warning');
		return false;

	},
	checkSelectOne: function (rows) {// 检查grid是否只勾选了一行,是返回 true,否返回true
		var records = rows;
		if (!Utils.checkSelect(records)) {
			return false;
		}
		if (records.length == 1) {
			return true;
		}
		Bdth.alert('警告', '只能选择一行记录.', 'messager-error');
		return false;
	}
}